package com.guochuang.yunwang.promanager.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guochuang.yunwang.promanager.dao.UserMapper;
import com.guochuang.yunwang.promanager.domain.User;
import com.guochuang.yunwang.promanager.domain.vo.UserSearchVO;
import com.guochuang.yunwang.promanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public Integer updateUserAvatorByUid(Integer uid, String headImg) {
        return userMapper.updateUserAvatorByUid(uid,headImg);
    }

    @Override
    @Transactional
    public Integer updateUserRoles(Integer uid, List<Integer> rolse) {

        // 首先删去该用户的所有角色
        Integer delroles = userMapper.delUserRoleByUid(uid);

        if(rolse.size()==0){
            return delroles;
        }

        // 将新的角色关系插入
        Integer nums = userMapper.insertUserRoles(uid,rolse);

        return nums;
    }

    @Override
    public Set<String> getPermissions(Integer uid) {

        return userMapper.selectPermissions(uid);
    }

    @Override
    public PageInfo findUserByParmas(UserSearchVO uservo) {

        PageHelper.startPage(uservo.getPage(),uservo.getOffset());

        List<User> users = userMapper.searchUserByParams(uservo);

        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }

    @Override
    public Integer delUserByUids(List<Integer> uids) {
        return userMapper.delUserByUids(uids);
    }

    @Override
    public PageInfo getUserByPage(Integer page, Integer offset) {

        PageHelper.startPage(page,offset);

        List<User> data = userMapper.findAllUsers();

        PageInfo<User> rec = new PageInfo<>(data);

        return rec;
    }

    @Override
    public User findUserByUID(Integer uid) {
        return userMapper.findUserByUID(uid);
    }

    @Override
    public User findUserByName(String username) {
        return userMapper.findUserByName(username);
    }

    @Override
    public User findUserByPhoneNum(String phonenum) {
        return userMapper.findUserByPhoneNum(phonenum);
    }

    @Override
    public User findUserByAccount(String account) {
        return userMapper.findUserByAccount(account);
    }

    @Override
    public List<User> findAllUsers(){
        return userMapper.findAllUsers();
    }

    @Override
    public Integer delUserByUId(Integer uid) {
        return userMapper.delUserByUID(uid);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }


    @Override
    public Integer addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public User getUserByAccount(String account) {
        return userMapper.findUserByAccount(account);
    }
}
